package com.example.video.controller;

import com.example.video.common.ResultModel;
import com.example.video.common.ResultStatus;
import com.example.video.domain.Device;
import com.example.video.domain.Information;
import com.example.video.service.inter.IInformationService;
import com.example.video.vo.NoticeVo;
import com.example.video.vo.UpdateVo;
import com.example.video.vo.VideoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Slf4j
@RestController
@RequestMapping("/information")
@Api(value = "消息相关接口", tags = {"InformationController"})
public class InformationController {

    private final IInformationService informationService;

    @Autowired
    public InformationController(IInformationService informationService) {
        this.informationService = informationService;
    }

    @RequiresUser
    @RequestMapping(method = POST)
    @ApiOperation(value = "创建/更新消息")
    public Object addOrUpdateInformation(@RequestBody Information information) {
        Long id = informationService.addOrUpdateInformation(information);
        return ResultModel.ok(id);
    }

    @RequiresUser
    @RequestMapping(method = DELETE)
    @ApiOperation(value = "删除消息")
    public Object deleteInformations(@RequestBody List<Long> informationIds) {
        informationService.deleteInformations(informationIds);
        return ResultModel.ok();
    }

    @RequiresUser
    @RequestMapping(method = GET)
    @ApiOperation(value = "获取消息列表", response = Information.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "page", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "size", paramType = "query")
    })
    public Object getInformationList(@RequestParam(value = "page", defaultValue = "0") int page,
                                     @RequestParam(value = "size", defaultValue = "10") int size) {
        List<Information> informations = informationService.getInformationList(page, size);
        if (CollectionUtils.isEmpty(informations))
            return ResultModel.error(ResultStatus.NO_DATA);
        return ResultModel.ok(informations);
    }

    @RequiresUser
    @RequestMapping(value = "/{information_id}", method = PUT)
    @ApiImplicitParam(dataType = "String", name = "information_id", value = "消息id", required = true, paramType = "path")
    @ApiOperation(value = "发布消息")
    public Object publishInformation(@PathVariable("information_id") Long informationId,
                                     @RequestBody List<String> deviceImeis) {
        informationService.publishInformation(informationId, deviceImeis);
        return ResultModel.ok();
    }

    @RequiresUser
    @RequestMapping(value = "/devices/{information_id}", method = GET)
    @ApiImplicitParam(dataType = "String", name = "information_id", value = "消息id", required = true, paramType = "path")
    @ApiOperation(value = "消息绑定的设备列表")
    public Object informationPublishedDevices(@PathVariable("information_id") Long informationId) {
        List<Device> devices = informationService.informationPublishedDevices(informationId);
        if (CollectionUtils.isEmpty(devices))
            return ResultModel.error(ResultStatus.NO_DATA);
        return ResultModel.ok(devices);
    }

    @RequestMapping(value = "/video/{IMEI}", method = GET)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "IMEI", value = "设备识别码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Long", name = "startTimestamp", value = "时间戳(查询此时间之后需要播放的视频,不传为当前时间)", paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "endTimestamp", value = "时间戳(查询此时间之前需要播放的视频,不传为null)", paramType = "query"),
    })
    @ApiOperation(value = " 查询设备视频")
    public Object queryVideo(@PathVariable("IMEI") String imei,
                             @RequestParam(value = "startTimestamp", defaultValue = "#{new java.util.Date().getTime()}") Long start,
                             @RequestParam(value = "endTimestamp", required = false) Long end) {
        List<VideoVo> videoVos = informationService.queryVideo(imei, start, end);
        if (CollectionUtils.isEmpty(videoVos))
            return ResultModel.error(ResultStatus.NO_DATA);
        return ResultModel.ok(videoVos);
    }

    @RequestMapping(value = "/notice/{IMEI}", method = GET)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "IMEI", value = "设备识别码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Long", name = "startTimestamp", value = "时间戳(查询此时间之后需要播放的视频,不传为当前时间)", paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "endTimestamp", value = "时间戳(查询此时间之前需要播放的视频,不传为null)", paramType = "query"),
    })
    @ApiOperation(value = " 查询设备公告")
    public Object queryNotice(@PathVariable("IMEI") String imei,
                              @RequestParam(value = "startTimestamp", defaultValue = "#{new java.util.Date().getTime()}") Long start,
                              @RequestParam(value = "endTimestamp", required = false) Long end) {
        List<NoticeVo> noticeVos = informationService.queryNotice(imei, start, end);
        if (CollectionUtils.isEmpty(noticeVos))
            return ResultModel.error(ResultStatus.NO_DATA);
        return ResultModel.ok(noticeVos);
    }

    @RequestMapping(value = "/update", method = GET)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "page", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "size", paramType = "query")
    })
    @ApiOperation(value = " 查询设备更新")
    public Object queryUpdate(@RequestParam(value = "page", defaultValue = "0") int page,
                              @RequestParam(value = "size", defaultValue = "1") int size) {
        List<UpdateVo> updateVos = informationService.queryUpdate(page, size);
        if (CollectionUtils.isEmpty(updateVos))
            return ResultModel.error(ResultStatus.NO_DATA);
        return ResultModel.ok(updateVos);
    }

}
